package com.cmcc.groupcontacts.firewall.adapter;

import java.util.ArrayList;
import java.util.Date;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.cmcc.groupcontacts.firewall.dao.model.CallListVO;
import com.cmcc.groupcontacts.util.DateUtil;
import com.cmcc.groupcontacts.util.ValidationUtil;
import com.yinhai.activity.R;

/**
 * 
 * 来电拦截列表Adapter
 * 
 * @author 
 * 
 */
public class CallListAdapter extends BaseAdapter<CallListVO> {
	/**
	 * 视图缓存
	 */
	public final class ViewCache {
		public TextView txtName;
		public TextView txtNumber;
		public TextView txtDate;
		public TextView txtType;
	}

	private ViewCache viewCache = null;

	public CallListAdapter(Context context, ArrayList<CallListVO> calls) {
		super(R.layout.call_list_item, context, calls);
	}

	@Override
	protected void setViewCache(View convertView, int position) {
		viewCache = new ViewCache();
		viewCache.txtName = (TextView) convertView.findViewById(R.id.txt_name);
		viewCache.txtNumber = (TextView) convertView.findViewById(R.id.txt_number);
		viewCache.txtDate = (TextView) convertView.findViewById(R.id.txt_date);
		viewCache.txtType = (TextView) convertView.findViewById(R.id.txt_type);
		convertView.setTag(viewCache);
	}

	@Override
	protected void getViewCache(View convertView, int position) {
		viewCache = (ViewCache) convertView.getTag();
	}

	@Override
	protected void setValueByViewCache(View convertView, int position) {
		final CallListVO call = getItem(position);
		if (!ValidationUtil.isNullOrEmpty(call.getName())) {
			viewCache.txtName.setText(call.getName());
		} else {
			viewCache.txtName.setText(call.getNumber());
		}
		// TODO: [优化]更改为显示归属地，将电话号码显示到联系人姓名后面（如果有的话）
		viewCache.txtNumber.setText(call.getNumber()); 
		viewCache.txtDate.setText(DateUtil.format2BeforeDatetime(new Date(call.getDate())));
		switch (call.getType()) {
		case CallListVO.TYPE_DISTURB:
			viewCache.txtType.setText(CallListVO.TYPE_DISTURB_TEXT);
			break;
		case CallListVO.TYPE_OTHER:
		default:
			viewCache.txtType.setText("");
			break;
		}
	}
}
